{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e0a1f2a-60ab-4001-9639-9d44589230b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['as']\n",
      "['d', 'a', 'd', 'a']\n",
      "10\n",
      "50\n",
      "2 1\n",
      "0 apple\n",
      "1 banana\n",
      "2 cherry\n",
      "0 apple\n",
      "1 banana\n",
      "2 cherry\n",
      "1 apple\n",
      "2 banana\n",
      "3 cherry\n"
     ]
    }
   ],
   "source": [
    "### 定义列表list\n",
    "l1 = [] # 定义一个空列表\n",
    "l1 = list() # 同样定义个空列表\n",
    "\n",
    "l1 = [\"as\"]\n",
    "l2 = list('dada')\n",
    "print(l1)\n",
    "print(l2)\n",
    "#元素访问\n",
    "#可以通过索引来访问列表中的元素，索引从0开始。例如，对于列表 l = [10, 20, 30]l[0] 会返回 10 。\n",
    "#也支持负索引，l[-1]会返回列表的最后一个元素，即 30 。\n",
    "#append()：在列表末尾添加一个元素。例如，l = [1, 2]执行 l.append(3) 后，l 变为 [1, 2, 3] 。\n",
    "#extend()：用于将一个可迭代对象（如列表、元组等）的元素添加到列表末尾。例如，l1 = [1, 2]，l2 = [3, 4]，执行\n",
    "# l1.extend(l2)后，1 变为 [1, 2, 3, 4] 。\n",
    "# insert()：在指定位置插入一个元素。语法为 list.insert(index, element) ，例如，l = [1, 3]，\n",
    "# 执行 l.insert(1, 2) 后，l 变为 [1, 2, 3]。\n",
    "#remove()：根据元素值删除列表中的元素。例如， = [1, 2, 2, 3]，执行 l.remove(2) 后\n",
    "# ，l变为 [1, 2, 3] ，只会删除第一个匹配的元素。\n",
    "#reverse将列表反转\n",
    "###索引和切片\n",
    "my_list = [10, 20, 30, 40, 50]\n",
    "print(my_list[0])\n",
    "print(my_list[-1])\n",
    "#切片\n",
    "#基本切片：my_list[1:3] ，\n",
    "#省略起始索引：my_list[:3] 等同于 my_list[0:3] ，结果是 [10, 20, 30] 。\n",
    "#设置步长：my_list[::2] ，步长为2，每隔一个元素取一个，结果是 [10, 30, 50]；my_list[1::2]\n",
    "#从索引1开始，步长为2，结果是\n",
    "# [ 20, 40] 。\n",
    "#负向步长（反转列表）：my_list[::-1] ，结果是 [50, 40, 30, 20, 10] 。\n",
    "# count()方法用于统计某个元素在序列中出现的次数\n",
    "# index()方法用于查找某个元素在序列中首次出现的索引位置\n",
    "print(l2.count('d'), l2.index('a'))\n",
    "#count方法：正常情况下不会报错，它会返回元素在列表中出现的次数，即使元素不存在，也会返回0。\n",
    "#index方法：如果列表 l2 中不存在 'e' 这个元素 ，调用\n",
    "# l2.index('e') 就会抛出 ValueError 异常，提示该元素不在列表中 。\n",
    "###enumerate函数\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "# 普通遍历\n",
    "for i in range(len(fruits)):\n",
    "    print(i, fruits[i])\n",
    "# 使用enumerate遍历\n",
    "for index, fruit in enumerate(fruits):\n",
    "    print(index, fruit)\n",
    "# 设置起始索引为1\n",
    "for index, fruit in enumerate(fruits, start=1):\n",
    "    print(index, fruit)\n",
    "#它会自动帮你把水果的位置（index ）和名称（fruit ）整理好，你直接使用就行，代码更简洁、直观 。\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa2889f9-0b93-431b-b962-cd901db4a760",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
